﻿using System.Data;
using System.Drawing;
using System;
using System.IO;

namespace Home.Haushalt.Lib.Element
{
    /// <summary>
    /// 
    /// </summary>
    [ElementExtension("Element")]
    public class FinDate : ElementBase
    {
        #region CONSTANTS
        public new static readonly Type ElementType = typeof(FinDate);

        /// <summary></summary>
        public class Fields
        {
            public const string EntryDate = "ed";
            public const string ValueDate = "vd";
            public const string EntryText = "et";
            public const string BankRef = "br";
            public const string CreditorId = "ci";
            public const string Currency = "c";
            public const string DecValue = "dv";
            public const string EndToEndId = "etei";
            public const string FundsCode = "fc";
            public const string MandateId = "mi";
            public const string OwnerRef = "or";
            public const string PackedPaymtPurpose = "ppp";
            public const string PayeePayerAcctNo = "ppan";
            public const string PayeePayerBankCode = "ppbc";
            public const string PayeePayerName1 = "ppn1";
            public const string PayeePayerName2 = "ppn2";
            public const string PrimaNotaNo = "pnn";
            public const string SupplementaryDetails = "sd";
            public const string TextKeyExt = "tke";
            public const string TranTypeIdCode = "ttic";
            public const string ZkaTranCode = "ztc";
            public const string New = "new";
        }
        #endregion CONSTANTS

        /// <summary></summary>
        public override string PropertyFormCaption { get { return (Row != null) ? Row[Fields.PayeePayerName1].ToString() : ""; } }


        /*if (elementType == FinDate.ElementType)
            {
                foreach (UltraGridCell cell in row.Cells)
                {
                    if (cell.Value is String && cell.Value.ToString().Length > 0)
                    {
                        string cellValue = cell.Value.ToString();
                        if (cellValue.Equals(cellValue.ToUpper()))
                        {
                            cell.Value = cellValue.Substring(0, 1).ToUpper() + cellValue.Substring(1, cellValue.Length - 1).ToLower();
                        }
                    }
                }
            }*/


        /// <summary></summary>
        public string FileName
        {
            get
            {
                return DAO.Settings.Selected.DataRelPath + "/" + FinDate.ElementType.Name.ToLower() + _finAccountID.ToString("00") + ".xml";
            }
        }

        /// <summary></summary>
        private int _finAccountID = -1;


        /// <summary></summary>
        private DataTable _finDateTable;

        /// <summary></summary>
        public override DataTable ElementTable
        {
            get
            {
                if (_finDateTable == null)
                {
                    if (_finAccountID == -1 && Row != null)
                    {
                        _finDateTable = Row.Table;
                    }
                    else
                    {
                        if (DAO.FinAccountTableDictionary.ContainsKey(_finAccountID))
                        {
                            _finDateTable = DAO.FinAccountTableDictionary[_finAccountID];
                        }
                        else
                        {
                            DataSet ds = new DataSet();
                            ds.OpenSchema(FinDate.ElementType);

                            if (!File.Exists(FileName))
                            {
                                ds.WriteXml(FileName, DAO.Credential.CurrentCry);
                                Journal.Add(this, ActionResult.Hinweis, "Datei [" + FileName + "] ist neu erstellt, da sie nicht vorhanden war");
                            }

                            try
                            {
                                ds.LoadXml(FileName, FinDate.ElementType, DAO.Credential.CurrentCry);
                                DAO.FinAccountTableDictionary.Add(_finAccountID, ds.Tables[0]);
                                _finDateTable = ds.Tables[0];
                                Journal.Add(this, "Datei [" + FileName + "] ist ausgelesen");
                            }
                            catch (Exception ex)
                            {
                                _finDateTable = null;
                                Journal.Add(this, "Datei [" + FileName + "] konnte nicht ausgelesen werden", ex);
                            }
                        }
                    }
                }
                return _finDateTable;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public FinDate() : base() { }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="parentDataRow"></param>
        public FinDate(DataRow parentDataRow)
            : base(parentDataRow)
        {
            _finDateTable = parentDataRow.Table;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="finAccountObj"></param>
        public FinDate(FinAccount finAccountObj)
        {
            _finAccountID = finAccountObj.ID;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override int Delete()
        {
            ElementTable.Rows.Remove(Row);

            return 0;
        }

        #region APPEARANCES

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override Color GetRowForeColor()
        {
            Color foreColor = base.GetRowForeColor();

            if (CheckColumnsInRow(Fields.DecValue))
            {
                if (Row[Fields.DecValue].ToDouble() >= 0)
                    foreColor = DAO.Settings.Selected.RowStateColorPlus;
                if (Row[Fields.DecValue].ToDouble() < 0)
                    foreColor = DAO.Settings.Selected.RowStateColorMinus;
            }

            return foreColor;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public override Color GetRowBackgroundColor()
        {
            Color backColor = base.GetRowBackgroundColor();

            if (CheckColumnsInRow(Fields.New))
            {
                if (Row[Fields.New].ToString().Length > 0)
                    backColor = Color.FromArgb(226, 255, 213);
            }

            return backColor;
        }

        #endregion APPEARANCES


    }
}
